package com.bankcomm.luafuction;

import org.keplerproject.luajava.LuaState;

import android.util.DisplayMetrics;

import com.bankcomm.common.ConfigManager;
import com.rytong.tools.ui.Component;
import com.rytong.tools.utils.Utils;

public class LuaScreen {

    public LuaScreen() {
        try {
            Component.LUASTATE.pushObjectValue(this);
            Component.LUASTATE.setGlobal("luascreen");
            String luahead = "screen = {}; ";
            String luafc1 = "function screen:reflash() luascreen:reflash(); end;";
            String luafc_width = "function screen:width() local width = luascreen:width(); return width; end;";
            String luafc_height = "function screen:height() local height = luascreen:height(); return height; end;";
            String luafc_dpi = "function screen:dpi() local dpi = luascreen:dpi(); return dpi; end;";
            Component.LUASTATE.gc(LuaState.LUA_GCCOLLECT, 0);
            Component.LUASTATE.LdoString(luahead.concat(luafc1).concat(luafc_width).concat(luafc_height)
                    .concat(luafc_dpi));
        } catch (Exception e) {
            Utils.printException(e);
        }
    }

    /**
     * @param L
     */
    public void reflash() {
        ConfigManager.currentView_.getWindow().getDecorView().invalidate();
    }

    /**
     * @return return the width of screen
     */
    public int width() {
        return Component.SCREENWIDTH;
    }

    /**
     * @return return the height of screen
     */
    public int height() {
        return Component.SCREENHEIGHT;
    }

    /**
     * @return return the Mobile phone screen dpi
     */
    public int dpi() {
        DisplayMetrics dm = new DisplayMetrics();
        ConfigManager.currentView_.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.densityDpi;
    }
}
